<template>
  <el-dialog
    :visible.sync="visible"
    title="服务补偿"
    width="60%"
    :before-close="handleClose"
  >
    <el-form
      :model="serviceCompensationForm"
      :rules="serviceCompensationFormRules"
      ref="serviceCompensationFormRef"
      label-width="110px"
    >
      <el-form-item label="补偿类型" prop="compensate_type">
        <el-select
          v-model="serviceCompensationForm.compensate_type"
          placeholder="请选择"
          filterable
          clearable
        >
          <el-option label="现金" value="1"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="补偿方式" prop="compensate_way">
        <el-select
          v-model="serviceCompensationForm.compensate_way"
          placeholder="请选择"
          filterable
          clearable
        >
          <el-option label="微信" value="1"> </el-option>
          <el-option label="支付宝" value="2"> </el-option>
          <el-option label="线下打款" value="3"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="补偿金额" prop="compensate_money">
        <el-input
          v-model="serviceCompensationForm.compensate_money"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        class="theme-background-color"
        @click="saveFormData"
        :disabled="isDisabled"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    serviceCompensationDialogVisible: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    orderId: {
      type: Number,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      serviceCompensationForm: {
        compensate_type: '',
        compensate_way: '',
        compensate_money: ''
      },
      serviceCompensationFormRules: {
        compensate_type: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        compensate_way: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        compensate_money: [
          { required: true, message: '请输入', trigger: 'blur' },
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: '请输入合法的金额数字，最多两位小数',
            trigger: 'blur'
          }
        ]
      },
      isDisabled: false
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('update:serviceCompensationDialogVisible', false)
          done()
        })
        .catch((_) => {})
    },
    closeDialog() {
      this.$emit('update:serviceCompensationDialogVisible', false)
    },
    saveFormData() {
      this.$refs.serviceCompensationFormRef.validate(async (valid) => {
        if (!valid) return
        this.serviceCompensationForm.order_booking_id = this.orderId
        console.log(this.serviceCompensationForm)

        this.isDisabled = true
        const { data: res } = await this.$http.post(
          '/admin/order_booking/compensate',
          this.serviceCompensationForm
        )
        this.isDisabled = false
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('操作成功')
        this.visible = false
        this.$emit('update:serviceCompensationDialogVisible', false)
        this.$parent.getDetailData()
      })
    }
  },
  watch: {
    serviceCompensationDialogVisible() {
      this.visible = this.serviceCompensationDialogVisible
      if (!this.visible) {
        this.$refs.serviceCompensationFormRef.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 80%;
  margin-left: 15px;
  .el-select {
    width: 100%;
  }
}
</style>
